package com.foreknow.web;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.User;
import com.foreknow.service.UserService;
import com.foreknow.utils.Constance;
import com.foreknow.utils.TokenUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController{
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public String login(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        HashMap<String,Object> hs=new HashMap<>();
        String username=(String)para.get("username");
        String password=(String)para.get("password");
        User userLogin = new User();
        userLogin.setUsername(username);
        userLogin.setPassword(password);
        User user = userService.login(userLogin);
        if (user==null){
            hs.put("meta",new Constance("密码错误！",201));
            ObjectMapper objectMapper=new ObjectMapper();
            return objectMapper.writeValueAsString(hs);
        }
        String token= TokenUtil.sign(user);
        user.setToken(token);
        hs.put("data",user);
        hs.put("meta",new Constance("登陆成功！",200));
        ObjectMapper objectMapper=new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }


    // 查询所有信息
    @RequestMapping(value = "/findUserByPage",method = RequestMethod.GET)
    @ResponseBody
    public String findUserByPage(@RequestParam Map<String,Object> para) throws JsonProcessingException {
        HashMap<String,Object> hs=new HashMap<>();

        String query =(String) para.get("query");
        Integer pageNum =Integer.parseInt((String) para.get("pageNum"));
        Integer pageSize =Integer.parseInt((String)  para.get("pageSize"));
        //设置分页信息
        PageHelper.startPage(pageNum,pageSize);
        List<User> userList = userService.getQueryLikeUserName(query);//只对第一个查询数据有效
    //PageInfo是PageHelper中内置的分页信息类
    PageInfo<User> info = new PageInfo(userList);
  //给map添加数据
        hs.put("total",info.getTotal());
        hs.put("pageNum",info.getPageNum());
        hs.put("user",userList);
        if (userList.size()>0){
            hs.put("meta",new Constance("获取成功",200));
        }else {
            hs.put("meta",new Constance("获取失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();
       return objectMapper.writeValueAsString(hs);
    }



 //根据条件查询信息
    @RequestMapping(value = "queryUser",method = RequestMethod.GET)
    @ResponseBody
    public String queryUser(@RequestParam Map<String,Object> para) throws JsonProcessingException{
        HashMap<String,Object> hs=new HashMap<>();
        //获取请求参数
        String userName = (String) para.get("userName");
        String realName =(String) para.get("realName");
        Integer useType =Integer.parseInt((String)para.get("useType"));
        Integer docTitleId =Integer.parseInt((String)para.get("docTitleId"));
        Integer deptId =Integer.parseInt((String)para.get("deptId"));
        Integer pageNum =Integer.parseInt((String) para.get("pageNum"));
        Integer pageSize =Integer.parseInt((String) para.get("pageSize"));
        //把请求参数传入user对象
        User user =new User();
        user.setUsername(userName);
        user.setRealname(realName);
        user.setUsetype(useType);
        user.setDoctitleid(docTitleId);
        user.setDeptid(deptId);
        List<User> userList = userService.getQueryUser(user);
        //设置分页信息
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<User> info =new PageInfo<>(userList);
        hs.put("user",userList);
        hs.put("total",info.getTotal());
        hs.put("pageNum",info.getPageNum());
        //添加meta
        if (userList.size()>0){
            hs.put("meta",new Constance("查询成功",200));
        }else {
            hs.put("meta",new Constance("查询失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    //根据id删除用户信息
    @RequestMapping(value = "deleteUserById" ,method = RequestMethod.GET)
    @ResponseBody
    public String deleteUserById(@RequestParam Map<String,Object>para) throws JsonProcessingException {
        HashMap<String,Object> hs=new HashMap<>();
        Integer id = Integer.parseInt((String)para.get("id"));
        int isRight = userService.deleteUserById(id);
        if (isRight==1){
            hs.put("meta",new Constance("删除成功",200));
        }else {
            hs.put("meta",new Constance("删除失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();

        return objectMapper.writeValueAsString(hs);
    }
    
    //批量删除用户信息
    @RequestMapping(value = "deleteByChoose",method = RequestMethod.POST)
    public String deleteByChoose(@RequestBody Map<String ,Object>para) throws JsonProcessingException {
        HashMap<String,Object> hs=new HashMap<>();
        List<Integer> arr = new ArrayList<Integer>();
       arr= (List<Integer>) para.get("info");


        int sum =0;
        int isRight =0;
        for(Integer ss:arr
             ) {
            isRight = userService.deleteUserById(ss);
            sum = sum + isRight;
        }
        if (sum==arr.size()){
            hs.put("meta",new Constance("批量删除成功",200));
        }else {
            hs.put("meta",new Constance("批量删除失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    //添加用户信息
    @RequestMapping(value = "insertUser",method = RequestMethod.POST)
    @ResponseBody
    public String insertUser(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        //获取表单信息
        HashMap<String,Object> hs = new HashMap<>();
        String userName = (String) para.get("username");
        String passWord = (String) para.get("passWord");
        String realName = (String) para.get("realName");
        Integer deptID  = (Integer) para.get("deptId");
        Integer useType = (Integer)para.get("userType");

        //传入User对象
        User user =new User();
        user.setUsername(userName);
        user.setPassword(passWord);
        user.setRealname(realName);
        user.setDeptid(deptID);
        user.setUsetype(useType);
        if(useType==3){
            Integer docTitleID = (Integer)para.get("docTitleId");
            user.setDoctitleid(docTitleID);
        }else {
            user.setDoctitleid(1);
        }
        user.setDelmark(1);
        //调用Service方法
        int isRight = userService.insertUser(user);
        if(isRight==1){
            hs.put("meta",new Constance("添加用户成功",200));
        }else {
            hs.put("meta",new Constance("添加用户失败",401));
        }
        ObjectMapper objectMapper =new ObjectMapper();

        return objectMapper.writeValueAsString(hs);
    }


    //根据id修改用户信息
    @RequestMapping(value = "updateUser" ,method = RequestMethod.POST)
    public String updateUserById(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        //获取表单信息
        HashMap<String,Object> hs = new HashMap<>();
        String userName = (String) para.get("username");
        String passWord = (String) para.get("passWord");
        String realName = (String) para.get("realName");
        Integer deptID  = (Integer) para.get("deptId");
        Integer useType = (Integer)para.get("userType");
        Integer userId = (Integer)para.get("userId");


        //传入User对象
        User user =new User();
        user.setUsername(userName);
        user.setPassword(passWord);
        user.setRealname(realName);
        user.setDeptid(deptID);
        user.setId(userId);
        user.setUsetype(useType);
        if(useType==3){
            Integer docTitleID = (Integer)para.get("docTitleId");
            user.setDoctitleid(docTitleID);
        }else {
            user.setDoctitleid(1);
        }
        user.setDelmark(1);

        //调用service修改方法
        int isRight = userService.updateUserById(user);
        if (isRight==1){
            hs.put("meta",new Constance("修改用户成功",200));
        }else {
            hs.put("meta",new Constance("修改用户失败",401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return  objectMapper.writeValueAsString(hs);
    }

    //根据id查询用户信息
    @RequestMapping(value = "selectUserById",method = RequestMethod.GET)
    public String selectUserById(@RequestParam Map<String,Object>para) throws JsonProcessingException {
        //获取表单信息
        HashMap<String,Object> hs = new HashMap<>();
        Integer id =Integer.parseInt((String) para.get("id"));
        User user = userService.selectUserById(id);
        hs.put("user",user);
        if (user!=null){
            hs.put("meta",new Constance("根据id获取用户信息成功",200));
        }else {
            hs.put("meta",new Constance("根据id获取用户信息失败",401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return  objectMapper.writeValueAsString(hs);
    }




//    @RequestMapping(value = "/test",method = RequestMethod.POST)
//    @ResponseBody
//    public String test(@RequestBody Map<String,Object> para) throws JsonProcessingException, JsonProcessingException {
//        HashMap<String,Object> hs=new HashMap<>();
//        hs.put("data","data");
//        ObjectMapper objectMapper=new ObjectMapper();
//        return objectMapper.writeValueAsString(hs);
//    }
}